package org.hfy;

import org.hfy.collections.helper.Symbol;
import org.hfy.collections.helper.SymbolString;

/**
 * @Description:
 * @Author: Takagi
 * @date: 2023/4/23
 */
public class Constants {
    public static final String LR0_ITEM = "·";
    public static final String LR1_ITEM = "·";
    public static final String SEARCH_SYMBOL_SEPARATOR = ",";
    public static final String BLANK = " ";
    public static final String PRODUCT = "->";
    public static final String SYMBOL_ALIAS = "'";
    public static final String EPSILON = "ε";
    public static final String EOF = "$";
    public static final String END = "\n\n";
    public static final String SHIFT_ACTION = "shift";
    public static final String REDUCTION_ACTION = "reduction";
    public static final String ACCEPT = "acc";
    public static final String REJECT = "reject";


    public static final Boolean SPLIT_SYMBOL = true;

    public static int COLUMN_WIDTH = 10;

    /*
     * @Author Takagi
     * @Description: 判断是否为终结符
     * @date: 2023/4/23
     * @param symbol
     * @return * @return boolean
     **/
    public static boolean isTerminator(Symbol symbol) {
        return !Constants.isNonTerminator(symbol);
    }

    /*
     * @Author Takagi
     * @Description: 判断是否为非终结符
     * @date: 2023/4/23
     * @param symbol
     * @return * @return boolean
     **/
    public static boolean isNonTerminator(Symbol symbol) {
        if (symbol == null || symbol.content.length() == 0) return false;
        return Character.isUpperCase(symbol.content.charAt(0));
    }

    /*
     * @Author Takagi
     * @Description: 是否为空串
     * @date: 2023/4/25
     * @param symbol
     * @return * @return boolean
     **/
    public static boolean isEpsilon(Symbol symbol) {
        if (symbol == null || symbol.content.length() == 0) return false;
        return symbol.content.equals(Constants.EPSILON);
    }

    public static boolean isEpsilon(SymbolString string) {
        if (string == null || string.symbolList.size() != 1) return false;
        return isEpsilon(string.getFirstSymbol());
    }

    /*
     * @Author Takagi
     * @Description: 判断是否未 LR0 项目的标志
     * @date: 2023/4/26
     * @param symbol
     * @return * @return boolean
     **/
    public static boolean isSearchSymbolSeparator(Symbol symbol) {
        if (symbol == null || symbol.content.length() == 0) return false;
        return symbol.content.equals(Constants.SEARCH_SYMBOL_SEPARATOR);
    }

    public static boolean isLR0Item(Symbol symbol) {
        if (symbol == null || symbol.content.length() == 0) return false;
        return symbol.content.equals(Constants.LR0_ITEM);
    }

    /*
     * @Author Takagi
     * @Description: 是否为移进状态
     * @date: 2023/4/26
     * @param actionType
     * @return * @return boolean
     **/
    public static boolean isShiftAction(Symbol actionType) {
        return actionType.content.equals(Constants.SHIFT_ACTION);
    }

    /*
     * @Author Takagi
     * @Description: 是否为归约状态
     * @date: 2023/4/26
     * @param actionType
     * @return * @return boolean
     **/
    public static boolean isReduceAction(Symbol actionType) {
        return actionType.content.equals(Constants.REDUCTION_ACTION);
    }

    public static String formatLine(String str) {
        return String.format("%-" + Constants.COLUMN_WIDTH + "s", str);
    }

    public static boolean isDigit(Symbol symbol) {
        String content = symbol.getContent();
        for (int i = 0; i < content.length(); ++i) {
            if (!Character.isDigit(content.charAt(i))) return false;
        }
        return true;
    }

    public static boolean isBracket(Symbol symbol) {
        return symbol.content.equals("(") || symbol.content.equals(")");
    }
}
